﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dropdownlist.Models;
using Dropdownlist.ViewModel;

namespace Dropdownlist.Controllers
{
    public class MenuController : Controller
    {
        // GET: Menu
        private Model1 db = new Model1();

        public ActionResult Index()
        {
            MenuViewModel menumode = new MenuViewModel();
            menumode.MenuLevel1 = db.Menus
                .Where(menu => menu.Parent == 0).ToList().Select(menu=>new SelectListItem
                {
                    Value = menu.Id.ToString(),
                    Text = menu.Name

                }).ToList();
            menumode.MenuLevel1.Insert(0, new SelectListItem
            {
                Value ="-1",
                Text ="Please select a menu"

            });
            menumode.MenuLevel2 = new List<SelectListItem>();
            return View("Index",menumode);
        }
        [HttpGet]
        public ActionResult FilterCatLevel2 (int id)
        {
            return Json(db.Menus.Where(c => c.Parent == id).ToList()
                .Select(c => new SelectListItem
                {
                    Value = c.Id.ToString(),
                    Text = c.Name
                }).ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult ListMenu()
        {
            var model = db.Menus;
            return View(model);
        }
    }
}